﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;

namespace Presentation
{
    public partial class TraCuuHanhKhach : Form
    {
        private string oldMaHK = null;
        private string oldHoTen = null;
        private bool oldGioiTinh;
        private string oldCMND = null;
        private string oldQuocTich = null;
        private string oldPassport = null;
        private string oldDiaChi = null;
        private string oldDienThoai = null;
        private DataGridViewCell oldCell;
        public TraCuuHanhKhach()
        {
            InitializeComponent();
        }

        private void Button_TraCuu_Click(object sender, EventArgs e)
        {
            bool result_ConvertIntoInt = new bool();

            //Kiểm tra TextBox_HoTenHK có rỗng hay không?
            if (TextBox_HoTenHK.Text.Trim() == "")
                MessageBox.Show("Xin vui lòng bổ sung họ tên hành khách!");

            //Kiểm tra TextBox_MaHK có rỗng hay không?
            if (TextBox_MaHK.Text.Trim() == "")
                MessageBox.Show("Xin vui lòng bổ sung mã hành khách!");

            //Kiểm tra TextBox_CMND có rỗng hay không?
            if (TextBox_CMND.Text.Trim() == "")
                MessageBox.Show("Xin vui lòng bổ sung số CMND của hành khách!");

            //Nếu 3 điều kiện trên thỏa, tiến hành tìm kiếm hành khách
            if ((TextBox_HoTenHK.Text.Trim() != "") && (TextBox_MaHK.Text.Trim() != "") && (TextBox_CMND.Text.Trim() != ""))
            {
                //Lấy thông tin từ TextBox_HoTenHK
                string hoTenHK = TextBox_HoTenHK.Text.Trim();
                TextBox_HoTenHK.Text = hoTenHK;

                //Lấy thông tin từ TextBox_MaHK
                string maHK = TextBox_MaHK.Text.Trim();
                TextBox_MaHK.Text = maHK;

                //Lấy thông tin từ TextBox_CMND
                string cmnd = TextBox_CMND.Text.Trim();
                TextBox_CMND.Text = cmnd;

                //Tiến hành tìm kiếm hành khách trong cơ sở dữ liệu
                List<HanhKhach_DTO> danhSachHanhKhach = new List<HanhKhach_DTO>();
                danhSachHanhKhach = HanhKhach_BUS.layHanhKhachTheoMaHK(maHK);
                if (danhSachHanhKhach == null)
                {
                    danhSachHanhKhach = HanhKhach_BUS.layHanhKhachTheoCMND(cmnd);
                    if (danhSachHanhKhach == null)
                    {
                        danhSachHanhKhach = HanhKhach_BUS.layHanhKhachTheoHoTen(hoTenHK);
                    }
                }

                GridView_TraCuuHK.Rows.Clear();
                for (int i = 0; i < danhSachHanhKhach.Count; i++)
                    GridView_TraCuuHK.Rows.Add(danhSachHanhKhach[i].MaHK, danhSachHanhKhach[i].HoTen, danhSachHanhKhach[i].GioiTinh, danhSachHanhKhach[i].CMND, danhSachHanhKhach[i].QuocTich, danhSachHanhKhach[i].Passport, danhSachHanhKhach[i].DiaChi, danhSachHanhKhach[i].DienThoai);
            }
        }

        private void Button_Xoa_Click(object sender, EventArgs e)
        {
            if (GridView_TraCuuHK.CurrentRow != null)
            {
                HanhKhach_DTO hanhKhach;
                //Lấy ra thông tin hành khách muốn xóa 
                string maHK = GridView_TraCuuHK.CurrentRow.Cells["Column_MaHK"].Value.ToString();
                string hoTen = GridView_TraCuuHK.CurrentRow.Cells["Column_HoTen"].Value.ToString();
                bool gioiTinh = GridView_TraCuuHK.CurrentRow.Cells["Column_GioiTinh"].Value.Equals(sender);
                string cmnd = GridView_TraCuuHK.CurrentRow.Cells["Column_CMND"].Value.ToString();
                string quocTich = GridView_TraCuuHK.CurrentRow.Cells["Column_QuocTich"].Value.ToString();
                string passport = GridView_TraCuuHK.CurrentRow.Cells["Column_Passport"].Value.ToString();
                string diaChi = GridView_TraCuuHK.CurrentRow.Cells["Column_DiaChi"].Value.ToString();
                string dienThoai = GridView_TraCuuHK.CurrentRow.Cells["Column_DienThoai"].Value.ToString();

                DialogResult dr = MessageBox.Show("Bạn có chắc là muốn xóa hành khách " + maHK + "?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                if (dr == DialogResult.Yes)
                {
                    hanhKhach.MaHK = maHK;
                    hanhKhach.HoTen = hoTen;
                    hanhKhach.GioiTinh = gioiTinh;
                    hanhKhach.CMND = cmnd;
                    hanhKhach.QuocTich = quocTich;
                    hanhKhach.Passport = passport;
                    hanhKhach.DiaChi = diaChi;
                    hanhKhach.DienThoai = dienThoai;

                    bool result = HanhKhach_BUS.xoaHanhKhach(hanhKhach);
                    if (result == true)
                    {
                        MessageBox.Show("Xóa thành công!", "Delete", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    }
                    else
                        MessageBox.Show("Xóa thất bại! Vui lòng chọn một hành khách khác!", "Delete", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                    MessageBox.Show("Mời bạn chọn một hành khách khác!", "Delete", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void GridView_TraCuuHK_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            oldMaHK = GridView_TraCuuHK.CurrentRow.Cells["Column_MaHK"].Value.ToString();
            oldHoTen = GridView_TraCuuHK.CurrentRow.Cells["Column_HoTen"].Value.ToString();
            oldGioiTinh = bool.Parse(GridView_TraCuuHK.CurrentRow.Cells["Column_GioiTinh"].Value.ToString());
            oldCMND = GridView_TraCuuHK.CurrentRow.Cells["Column_CMND"].Value.ToString();
            oldQuocTich = GridView_TraCuuHK.CurrentRow.Cells["Column_QuocTich"].Value.ToString();
            oldPassport = GridView_TraCuuHK.CurrentRow.Cells["Column_Passport"].Value.ToString();
            oldDiaChi = GridView_TraCuuHK.CurrentRow.Cells["Column_DiaChi"].Value.ToString();
            oldDienThoai = GridView_TraCuuHK.CurrentRow.Cells["Column_DienThoai"].Value.ToString();
        }

        private void GridView_TraCuuHK_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            string newMaHK = null;
            string newHoTen = null;
            bool newGioiTinh = false;
            string newCMND = null;
            string newQuocTich = null;
            string newPassport = null;
            string newDiaChi = null;
            string newDienThoai = null;
            HanhKhach_DTO tmp = new HanhKhach_DTO();

            //Ô họ tên
            if (oldCell.OwningRow.Cells[1].Value == null)
            {
                oldCell.OwningRow.Cells[1].Value = oldHoTen;
                newHoTen = oldHoTen;
            }
            else
            {
                newHoTen = oldCell.OwningRow.Cells[1].Value.ToString().Trim();
                oldCell.OwningRow.Cells[1].Value = newHoTen;
            }

            //Ô giới tính
            if (oldCell.OwningRow.Cells[2].Value == null)
            {
                oldCell.OwningRow.Cells[2].Value = oldGioiTinh.ToString();
                newGioiTinh = oldGioiTinh;
            }
            else
            {
                if (String.Compare(oldCell.OwningRow.Cells[2].Value.ToString(), "true") == 0)
                    newGioiTinh = true;
                if (String.Compare(oldCell.OwningRow.Cells[2].Value.ToString(), "false") == 0)
                    newGioiTinh = false;
                oldCell.OwningRow.Cells[2].Value = newGioiTinh.ToString();
            }

            //Ô cmnd
            if (oldCell.OwningRow.Cells[3].Value == null)
            {
                oldCell.OwningRow.Cells[3].Value = oldCMND;
                newCMND = oldCMND;
            }
            else
            {
                newCMND = oldCell.OwningRow.Cells[3].Value.ToString().Trim();
                oldCell.OwningRow.Cells[3].Value = newCMND;
            }

            //Ô Quốc tịch
            if (oldCell.OwningRow.Cells[4].Value == null)
            {
                oldCell.OwningRow.Cells[4].Value = oldQuocTich;
                newQuocTich = oldQuocTich;
            }
            else
            {
                newQuocTich = oldCell.OwningRow.Cells[4].Value.ToString().Trim();
                oldCell.OwningRow.Cells[4].Value = newQuocTich;
            }

            //Ô Passport
            if (oldCell.OwningRow.Cells[5].Value == null)
            {
                oldCell.OwningRow.Cells[5].Value = oldPassport;
                newPassport = oldPassport;
            }
            else
            {
                newPassport = oldCell.OwningRow.Cells[5].Value.ToString().Trim();
                oldCell.OwningRow.Cells[5].Value = newPassport;
            }

            //Ô địa chỉ
            if (oldCell.OwningRow.Cells[6].Value == null)
            {
                oldCell.OwningRow.Cells[6].Value = oldDiaChi;
                newDiaChi = oldDiaChi;
            }
            else
            {
                newDiaChi = oldCell.OwningRow.Cells[6].Value.ToString().Trim();
                oldCell.OwningRow.Cells[6].Value = newDiaChi;
            }

            //Ô điện thoại
            if (oldCell.OwningRow.Cells[7].Value == null)
            {
                oldCell.OwningRow.Cells[7].Value = oldDienThoai;
                newDienThoai = oldDienThoai;
            }
            else
            {
                newDienThoai = oldCell.OwningRow.Cells[7].Value.ToString().Trim();
                oldCell.OwningRow.Cells[7].Value = newDienThoai;
            }

            tmp.MaHK = oldCell.OwningRow.Cells[0].Value.ToString();
            tmp.HoTen = newHoTen;
            tmp.GioiTinh = newGioiTinh;
            tmp.QuocTich = newQuocTich;
            tmp.CMND = newCMND;
            tmp.Passport = newPassport;
            tmp.DiaChi = newDiaChi;
            tmp.DienThoai = newDienThoai;
            HanhKhach_BUS.suaHanhKhach(tmp);
        }

    }
}
